package com.ykw.parking.controller;

import com.ykw.parking.mapper.ParkingMapper;
import com.ykw.parking.pojo1.Parking;
import com.ykw.parking.pojo1.ParkingLot;
import com.ykw.parking.pojo1.ParkingOne;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class FindParkingController {
    @Autowired
    private ParkingMapper parkingMapper;
    @RequestMapping("/findparking")
    public String find(String keyword,String flag,Model model){
        if (flag.equals("0")){
            model.addAttribute("keyword",keyword);
            ArrayList<Parking> parkings;
            ArrayList<ParkingOne> parking=new ArrayList<>();
            parkings = parkingMapper.queryByAddress(keyword);
            for (Parking i : parkings){
                ArrayList<ParkingLot> parkingLots = parkingMapper.queryParkingLotByPid(String.valueOf(i.getId()));
                for (ParkingLot pl:parkingLots){
                    parking.add(new ParkingOne(String.valueOf(i.getId()),pl.getId(),i.getAddress(),pl.getPrice()));
                }
            }
            model.addAttribute("currentPage",1);
            model.addAttribute("parkingLength",parking.size());
            model.addAttribute("page",Math.ceil(parking.size()/5.0));
            if(parkings.size()>5){
                model.addAttribute("parkingList",parking.subList(0,5));
            }else{
                model.addAttribute("parkingList",parking);
            }
            return "buying";
        }else{
            model.addAttribute("keyword",keyword);
            ArrayList<Parking> parkings;
            ArrayList<ParkingOne> parking=new ArrayList<>();
            parkings = parkingMapper.queryByAddress(keyword);
            for (Parking i : parkings){
                ArrayList<ParkingLot> parkingLots = parkingMapper.queryParkingLotByPid1(String.valueOf(i.getId()));
                for (ParkingLot pl:parkingLots){
                    parking.add(new ParkingOne(String.valueOf(i.getId()),pl.getId(),i.getAddress(),pl.getPrice()));
                }
            }
            model.addAttribute("currentPage",1);
            model.addAttribute("parkingLength",parking.size());
            model.addAttribute("page",Math.ceil(parking.size()/5.0));
            if(parkings.size()>5){
                model.addAttribute("parkingList",parking.subList(0,5));
            }else{
                model.addAttribute("parkingList",parking);
            }
            return "renchou";
        }

    }
    @RequestMapping("/findparkingparkinglist")
    public String find1(String keyword,Integer page,Model model){
        List<Parking> parkings = parkingMapper.queryByAddress(keyword);
        ArrayList<ParkingOne> parking=new ArrayList<>();
        for (Parking i : parkings){
            ArrayList<ParkingLot> parkingLots = parkingMapper.queryParkingLotByPid(String.valueOf(i.getId()));
            for (ParkingLot pl:parkingLots){
                parking.add(new ParkingOne(String.valueOf(i.getId()),pl.getId(),i.getAddress(),pl.getPrice()));
            }
        }
        model.addAttribute("keyword",keyword);
        model.addAttribute("currentPage",page);
        model.addAttribute("page",Math.ceil(parking.size()/5.0));
        model.addAttribute("parkingLength",parking.size());
        if(page*5>parking.size()){//进行一个简单的分页
             model.addAttribute("parkingList",parking.subList((page-1)*5,parking.size()));
             return "buying";
        }else{
              model.addAttribute("parkingList",parking.subList((page-1)*5,page*5));
              return "buying";
        }
    }

    @RequestMapping("/rcparkinglist")
    public String rcparkinglist(String keyword,Integer page,Model model){
        List<Parking> parkings = parkingMapper.queryByAddress(keyword);
        ArrayList<ParkingOne> parking=new ArrayList<>();
        for (Parking i : parkings){
            ArrayList<ParkingLot> parkingLots = parkingMapper.queryParkingLotByPid1(String.valueOf(i.getId()));
            for (ParkingLot pl:parkingLots){
                parking.add(new ParkingOne(String.valueOf(i.getId()),pl.getId(),i.getAddress(),pl.getPrice()));
            }
        }
        model.addAttribute("keyword",keyword);
        model.addAttribute("currentPage",page);
        model.addAttribute("page",Math.ceil(parking.size()/5.0));
        model.addAttribute("parkingLength",parking.size());
        if(page*5>parking.size()){//进行一个简单的分页
            model.addAttribute("parkingList",parking.subList((page-1)*5,parking.size()));
            return "renchou";
        }else{
            model.addAttribute("parkingList",parking.subList((page-1)*5,page*5));
            return "renchou";
        }
    }
    @RequestMapping("/ProductDetails")
    public String ProductDetails(@RequestParam("ParkingId") String ParkingId, Model model){
        ParkingLot parkingLot = parkingMapper.queryParkingLotById(Integer.valueOf(ParkingId));
        Parking parking = parkingMapper.queryByParkingId(parkingLot.getPid());
        model.addAttribute("parkinglot",parkingLot);
        model.addAttribute("parking",parking);
        return "test";
    }

    @RequestMapping("/RenchouProduct")
    public String RenchouProduct(@RequestParam("ParkingId") String ParkingId, Model model){
        ParkingLot parkingLot = parkingMapper.queryParkingLotById(Integer.valueOf(ParkingId));
        Parking parking = parkingMapper.queryByParkingId(parkingLot.getPid());
        model.addAttribute("parkinglot",parkingLot);
        model.addAttribute("parking",parking);
        return "product";
    }


}
